﻿using GodSystemCommon;
using GodSystemModel;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GodSystemDAL
{
    public class ProductsDAL
    {
        public object CheckProductSn(ProductsModel product)
        {
            string sql = "select count(*) as total from products where ProductSn = @ProductSn";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@ProductSn",product.ProductSn),
            };
            return SqlHelper.ExecuteScalar(sql, pms);
        }


        public DataTable GetProductsList(int currentPage, int pageSize = 4, ProductsModel product = null)
        {
            int startNum = (currentPage - 1) * pageSize;
            if (product != null)
            {
                
            }
            MySqlParameter[] pms = new MySqlParameter[2];
            pms[0] = new MySqlParameter("@startNum", startNum);
            pms[1] = new MySqlParameter("@pageSize", pageSize);
            //string where = " where 1 ";
            //if (!string.IsNullOrEmpty(name))
            //{
            //    where += " and Name like @Name ";
            //    pms[2] = new MySqlParameter("@Name", "%" + name + "%");
            //}


            string sql = "select Id,ProductName,ProductSn,SupplierId,ProductStatus,CreateTime,CateId,MainSku,Weight," +
                "PurchasePrice,ProductThumbImage from Products  order by Id desc limit @startNum, @pageSize";
            return SqlHelper.ExecuteDataTable(sql, pms);
        }


        public int AddProduct(ProductsModel product)
        {
            string sql = "insert into products(ProductName,ProductSn,SupplierId,ProductStatus,CreateTime,CateId,MainSku,Weight," +
                "PurchasePrice,ProductThumbImage) values(@ProductName,@ProductSn,@SupplierId,@ProductStatus," +
                "@CreateTime,@CateId,@MainSku,@Weight,@PurchasePrice,@ProductThumbImage)";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@ProductName",product.ProductName),
                new MySqlParameter("@ProductSn",product.ProductSn),
                new MySqlParameter("@SupplierId",product.SupplierId),
                new MySqlParameter("@ProductStatus",product.ProductStatus),
                new MySqlParameter("@CreateTime",product.CreateTime),
                new MySqlParameter("@CateId",product.CateId),
                new MySqlParameter("@MainSku",product.MainSku),
                new MySqlParameter("@Weight",product.Weight),
                new MySqlParameter("@PurchasePrice",product.PurchasePrice),
                new MySqlParameter("@ProductThumbImage",product.ProductThumbImage),
            };
            return SqlHelper.ExecuteNonQuery(sql, pms);
        }
    }
}
